library(plotly)
library(dplyr)
# https://ourworldindata.org/grapher/share-plastic-waste-recycled?tab=table
RecycleData <- read.csv("C:/Users/jenni/OneDrive/Desktop/DataVisFP/recycling.csv") # Countries and how much more they're recycling
I would like to try my hand at that animated line graph you made.
plot_ly(
data = RecycleData,
x = ~Year,
y = ~RecyclePercentage,
type = 'scatter',
mode = 'lines'
)
Basic, it displays the percentage of solid waste recycled by country. You can’t really tell what country is what right now. Also without a title it’s hard to discern the purpose upon first glance.
plot_ly(
data = RecycleData,
x = ~Year,
y = ~RecyclePercentage,
split = ~Entity, # Splitting lines by entity
type = 'scatter',
mode = 'lines',
color = ~Entity
) %>%
layout(
title = list(
text = "Improvement in Recycling Over Years",
font = list(size = 20),
x = 0.5,
xanchor = "center"
),
xaxis = list(title = "Year"),
yaxis = list(title = "Recycling Percentage (%)")
)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
There’s a title now and axis labels that display the unit. The lines are also now colored by what bigger entity / country they represent.
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
accumulated_df <- RecycleData %>%
accumulate_by(~Year)
plot_ly(
data = accumulated_df,
x = ~Year,
y = ~RecyclePercentage,
split = ~Entity,
frame = ~frame,
type = 'scatter',
mode = 'lines',
text = ~Entity, # Adding hover text
hoverinfo = "text+x+y"
) %>%
layout(
title = list(
text = "Improvement in Recycling Over Years",
font = list(size = 20),
x = 0.5,
xanchor = "center"
),
xaxis = list(title = "Year"),
yaxis = list(title = "Recycling Percentage (%)")
) %>%
animation_opts(frame = 100, transition = 50, redraw = FALSE)
I have not made progressively revealing plot before and I think it would be a very positive and hopeful way to show an improvement to a rather downer issue. Showing that we as a whole have made a tangible and numeric improvement to our habits is inspiring and I believe that the slow reveal is very encouraging and expresses that message in it’s execution.
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
# Accumulate the data by year
accumulated_df <- RecycleData %>%
accumulate_by(~Year)
# Create the animated plot with adjusted margins and size
plot_ly(
data = accumulated_df,
x = ~Year,
y = ~RecyclePercentage,
split = ~Entity,
frame = ~frame,
type = 'scatter',
mode = 'lines',
text = ~Entity, # hover text
hoverinfo = "text+x+y"
) %>%
layout(
title = list(
text = "Improvement in Recycling Over Years",
font = list(size = 20),
x = 0.5,
xanchor = "center",
y = 0.9 # Shift title
),
xaxis = list(
title = "Year",
titlefont = list(size = 14),
zeroline = FALSE
),
yaxis = list(
title = "Recycling Percentage (%)",
range = c(0, 20), # y-axis range
zeroline = FALSE
),
margin = list(
t = 60, # Top margin
b = 50, # Bottom margin
l = 50, # Left margin
r = 50 # Right margin
),
width = 800, # Adjust overall width
height = 450 # Adjust overall height
) %>%
animation_opts(frame = 100, transition = 50, redraw = FALSE)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
The y-range was adjusted to be more honest. It should start from 0 and 15 is not the upper limit or ideal percentage of recycling. There is room for improvement.